package com.think.teaching.controller;


import com.think.common.controller.BaseController;
import com.think.common.model.Result;
import com.think.common.model.ResultCode;
import com.think.common.utils.Common;
import com.think.teaching.model.CourseUser;
import com.think.teaching.model.StudentInfo;
import com.think.teaching.service.impl.CourseUserServiceImpl;
import com.think.teaching.service.impl.StudentInfoServiceImpl;
import com.think.teaching.service.impl.TeacherInfoServiceImpl;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 用户课程 前端控制器
 * </p>
 *
 * @author think
 * @since 2020-08-12
 */
@RestController
@RequestMapping("/v1")
@Api(tags = "用户课程接口文档")
public class CourseUserController extends BaseController{

	private final CourseUserServiceImpl courseUserService;
	
	private final StudentInfoServiceImpl studentInfoServiceImpl;

	public CourseUserController(CourseUserServiceImpl courseUserService,StudentInfoServiceImpl studentInfoServiceImpl) {
		this.courseUserService = courseUserService;
		this.studentInfoServiceImpl = studentInfoServiceImpl;
	}

	@ApiOperation("添加新的用户课程")
	@PostMapping("admin/courseUser")
	public Result AddCourseUser(@RequestBody()CourseUser data) {
		Result result = data.CheckObjectComplete();
		if(result.getCode() == ResultCode.SUCCESS.code()) {
			CourseUser courseUser = courseUserService.AddCourseUser(data);
			if( courseUser!= null) {
				return Result.success(courseUser);
			}
			else {
				return Result.failure(ResultCode.SYSTEM_INNER_ERROR);
			}
		}
		else
			return result;
	}
	
	@ApiOperation("更新用户课程")
	@RequestMapping(value = "admin/courseUser",method = RequestMethod.PUT)
	public Result UpdateCourseUser(@RequestBody()CourseUser data) {
		Result result = data.CheckObjectComplete();
		if(result.getCode() == ResultCode.SUCCESS.code()) {
			CourseUser courseUser = courseUserService.UpdateCourseUser(data);
			if( courseUser!= null) {
				return Result.success(courseUser);
			}
			else {
				return Result.failure(ResultCode.SYSTEM_INNER_ERROR);
			}
		}
		else
			return result;
	}
	
	@ApiOperation("删除用户课程")
	@RequestMapping(value = "admin/courseUser/{id}",method =
			RequestMethod.DELETE)
	public Result DeleteCourseUser(@PathVariable("id")Integer id) {
		CourseUser courseUser = courseUserService.getById(id);
		if( courseUser== null) {
			return Result.failure(ResultCode.COURSE_USER_NOT_EXIST);
		}
		else {
			if (courseUserService.DeleteObjectById(id)) {
				return Result.success();
			}
			return Result.failure(ResultCode.SYSTEM_INNER_ERROR);
		}
	}
	
	@ApiOperation("管理员获取用户课程详情")
	@RequestMapping(value = "admin/courseUser/{id}",method = RequestMethod.GET)
	public Result GetCourseUser(@PathVariable("id")Integer id) {
		CourseUser courseUser = courseUserService.getById(id);
		if( courseUser== null) {
			return Result.failure(ResultCode.COURSE_USER_NOT_EXIST);
		}
		else {
			
			return Result.success(courseUser);
		}
	}
	@ApiOperation("获取用户选课详情")
	@RequestMapping(value = "user/courseUser",method = RequestMethod.GET)
	public Result GetUserHadCourse(@RequestParam("course")Integer courseId) {
		StudentInfo studentInfo = studentInfoServiceImpl.GetStudentInfoByUserId(Common.GetCurrentAuthor().getUserId());
		if (studentInfo == null) {
			return Result.failure(ResultCode.STUDENT_NOT_EXIST);
		}
		return Result.success(courseUserService.GetByUserIDAndCourseId(courseId, studentInfo.getId()));
	}
	
	@ApiOperation("用户搜索用户课程")
	@RequestMapping(value = "user/courseUser/search",method = RequestMethod.GET)
	public Result UserSearchCourseUser(@RequestParam("key")String key) {
		return Result.success(courseUserService.SearchCourseUserWithKey(key, true));
	}
	
	@ApiOperation("管理员搜索用户课程")
	@RequestMapping(value = "admin/courseUser/search",method = RequestMethod.GET)
	public Result AdminSearchCourseUser(@RequestParam("key")String key) {
		return Result.success(courseUserService.SearchCourseUserWithKey(key, false));
	}
	
	@ApiOperation("管理员获取用户课程列表")
	@RequestMapping(value = "admin/courseUsers/{page}/{count}",method = RequestMethod.GET)
	public Result AdminListCourseUser(@PathVariable("page")Integer page,@PathVariable("count")Integer count,@RequestParam(required = false,defaultValue = "-1")Integer course) {
		return Result.success(courseUserService.ListCourseUser(count, page, course));
	}
	
	@ApiOperation("用户获取用户课程列表")
	@RequestMapping(value = "user/courseUsers/{page}/{count}",method = RequestMethod.GET)
	public Result UserListCourseUser(@PathVariable("page")Integer page,@PathVariable("count")Integer count,@RequestParam(required = false,defaultValue = "-1")Integer course) {


		return Result.success(courseUserService.ListUserSelectCourse(count, page));
	}
}

